import 'package:flutter/services.dart';
import 'package:flutter/material.dart';

class NinePatchFlutterView extends StatefulWidget {
  final String? url;
  final VoidCallback onLoaded;

  const NinePatchFlutterView(
      {super.key, required this.url, required this.onLoaded});

  @override
  State<NinePatchFlutterView> createState() => _NinePatchFlutterViewState();
}

class _NinePatchFlutterViewState extends State<NinePatchFlutterView> {
  MethodChannel? _channel;

  @override
  Widget build(BuildContext context) {
    return AndroidView(
      viewType: 'nine_patch_flutter_view',
      creationParams: {'url': widget.url},
      onPlatformViewCreated: (id) {
        _channel = MethodChannel('nine_patch_flutter_view_$id');
        _channel?.setMethodCallHandler((call) async {
          if (call.method == 'onLoaded') {
            setState(() {
              widget.onLoaded();
            });
          }
        });
      },
      creationParamsCodec: const StandardMessageCodec(),
    );
  }
}
